﻿#include "ToolbarNosepiece.h"
#include <QDebug>
ToolBarNosepiece::ToolBarNosepiece()
    :m_lens(-1)
{
    setIconSize(QSize(32, 32));
    initContent();
    initConnection();
}

void ToolBarNosepiece::initContent()
{
    actionLens0 = new QAction(this);
    actionLens0->setObjectName(QStringLiteral("actionLen1"));
    actionLens0->setCheckable(true);
    QIcon icon1Len1;
    icon1Len1.addFile(QStringLiteral(":/NosepieceCommon/res/x2_5.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionLens0->setIcon(icon1Len1);
    addAction(actionLens0);

    actionLens1 = new QAction(this);
    actionLens1->setObjectName(QStringLiteral("actionLen2"));
    actionLens1->setCheckable(true);
    QIcon icon1Len2;
    icon1Len2.addFile(QStringLiteral(":/NosepieceCommon/res/x5.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionLens1->setIcon(icon1Len2);
    addAction(actionLens1);

    actionLens2 = new QAction(this);
    actionLens2->setObjectName(QStringLiteral("actionLen3"));
    actionLens2->setCheckable(true);
    QIcon icon1Len3;
    icon1Len3.addFile(QStringLiteral(":/NosepieceCommon/res/x10.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionLens2->setIcon(icon1Len3);
    addAction(actionLens2);

    actionLens3 = new QAction(this);
    actionLens3->setObjectName(QStringLiteral("actionLen4"));
    actionLens3->setCheckable(true);
    QIcon icon1Len4;
    icon1Len4.addFile(QStringLiteral(":/NosepieceCommon/res/x20.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionLens3->setIcon(icon1Len4);
    addAction(actionLens3);

    actionLens4 = new QAction(this);
    actionLens4->setObjectName(QStringLiteral("actionLen5"));
    actionLens4->setCheckable(true);
    QIcon iconLen5;
    iconLen5.addFile(QStringLiteral(":/NosepieceCommon/res/x50.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionLens4->setIcon(iconLen5);
    addAction(actionLens4);
    actionLens4->setVisible(true);

    actionLensHome = new QAction(this);
    actionLensHome->setObjectName(QStringLiteral("actionLenHome"));
    actionLensHome->setCheckable(true);
    QIcon iconLenHome;
    iconLenHome.addFile(QStringLiteral(":/NosepieceCommon/res/x5-Home.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionLensHome->setIcon(iconLenHome);
    addAction(actionLensHome);
    actionLensHome->setVisible(false);

    setWindowTitle("Lens Select Toolbar");
}

void ToolBarNosepiece::initConnection()
{
    connect(actionLens0, SIGNAL(triggered()), this, SLOT(lensUpdate(void)));
    connect(actionLens1, SIGNAL(triggered()), this, SLOT(lensUpdate(void)));
    connect(actionLens2, SIGNAL(triggered()), this, SLOT(lensUpdate(void)));
    connect(actionLens3, SIGNAL(triggered()), this, SLOT(lensUpdate(void)));
    connect(actionLens4, SIGNAL(triggered()), this, SLOT(lensUpdate(void)));
    connect(actionLensHome, SIGNAL(triggered()), this, SLOT(lensHomeUI(void)));
}

void ToolBarNosepiece::lensHomeUI()
{
    setLens(0);
    emit doLensHome();
}

void ToolBarNosepiece::lensUpdate(void)
{
    QAction *act = (QAction *) sender();
    QAction *actionArray[5];
    actionArray[0] = actionLens0;
    actionArray[1] = actionLens1;
    actionArray[2] = actionLens2;
    actionArray[3] = actionLens3;
    actionArray[4] = actionLens4;

    int lens;

    act->setChecked(true);
    for(int i = 0; i <= 4; i++)
    {
        if(act == actionArray[i] )
        {
            lens = i;
            if(m_lens == lens)
            {
                break;
            }
            // 表示真正切换了物镜
            int oldLens = m_lens; // 这里要记录 oldlens, 因为 setLens 会改变 m_lens
            setLens(lens);
            emit changeLens(oldLens, lens);
            break;
        }
    }

}

void ToolBarNosepiece::setLens(int oldLens, int newLens)
{
    qDebug() << "ToolBarNosepiece::setLens(int oldLens, int newLens)";
    qDebug() << "oldLens = " << oldLens << ", newLens = " << newLens;

    Q_UNUSED(oldLens);
    setLens(newLens);
}

void ToolBarNosepiece::setLens(int lens)
{
    if(lens > 4 || lens < -1) return;
    if(lens == -1)
    {
        actionLens0->setChecked(false);
        actionLens1->setChecked(false);
        actionLens2->setChecked(false);
        actionLens3->setChecked(false);
        actionLens4->setChecked(false);
        return;
    }

    if(lens == m_lens) return;

    actionLens0->setChecked(false);
    actionLens1->setChecked(false);
    actionLens2->setChecked(false);
    actionLens3->setChecked(false);
    actionLens4->setChecked(false);

    switch(lens)
    {
    case 0: actionLens0->setChecked(true);
        break;
    case 1: actionLens1->setChecked(true);
        break;
    case 2: actionLens2->setChecked(true);
        break;
    case 3: actionLens3->setChecked(true);
        break;
    case 4: actionLens4->setChecked(true);
        break;
    default:
        break;
    }
    m_lens = lens;
}
